﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Brain.Windows;
using Microsoft.Xna.Framework.Graphics;

namespace Brain.Editor
{
    public partial class DatabaseList : UserControl
    {
        public Database Database;

        private Type type_string;

        public DatabaseList(Database database)
        {
            type_string = typeof(string);

            this.Database = database;
            InitializeComponent();

            UpdateDatabase();
        }

        public void UpdateDatabase()
        {
            this.listBox1.Items.Clear();

            for (int x = 0; x < Database.Variables.Count; x++)
            {
                IGameVariable var = Database.Variables[x];
                ObjectHolder holder = new ObjectHolder();
                holder.Object = var;
                holder.String = var.Name;
                listBox1.Items.Add(holder);
            }
        }

        public IGameVariable SelectedVariable;
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                this.flowLayoutPanel1.Controls.Clear();

                ObjectHolder var = listBox1.SelectedItem as ObjectHolder;
                SelectedVariable = var.Object as IGameVariable;

                Type t = var.Object.GetType();
                PropertyInfo[] props = t.GetProperties();

                foreach (PropertyInfo info in props)
                {
                    object[] att = info.GetCustomAttributes(true);
                    if (att != null && att.Length > 0)
                    {
                        for (int i = 0; i < att.Length; i++)
                        {
                            object o = att[i];
                            if (o is TexturePlace)
                            {
                                TextureControl con = new TextureControl();
                                con.Tag = info;
                                con.MouseDoubleClick += new MouseEventHandler(con_MouseDoubleClick);
                                this.flowLayoutPanel1.Controls.Add(con);
                                string value = (string)info.GetValue(var.Object, null);
                                if (!string.IsNullOrEmpty(value))
                                {
                                    con.Texture = Engine.Instance.ContentManager.Load<Texture2D>(value);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (info.PropertyType == type_string)
                        {
                            LabelTextBox con = new LabelTextBox();
                            con.TextBox.Text = (string)info.GetValue(var.Object, null);
                            con.LabelText.Text = info.Name;
                            con.Tag = info;
                            con.TextChanged += new SetObject(box_TextChanged);
                            this.flowLayoutPanel1.Controls.Add(con);
                        }
                    }
                }
            }
        }

        void con_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            TextureControl c = (TextureControl)sender;
            AssetManager manager = new AssetManager(true);
            if (manager.ShowDialog() == DialogResult.OK)
            {
                ContentProjectFile file = manager.SelectedFile;
                string final = "confile:" + file.Name;
                c.Texture = Engine.Instance.ContentManager.Load<Texture2D>(final);
                PropertyInfo info = c.Tag as PropertyInfo;
                info.SetValue(this.SelectedVariable, final, null);
            }
        }

        void box_TextChanged(object o)
        {
            LabelTextBox box = o as LabelTextBox;
            PropertyInfo info = box.Tag as PropertyInfo;
            info.SetValue(SelectedVariable, box.TextBox.Text, null);

            UpdateDatabase();
        }

        private void button_Add_Click(object sender, EventArgs e)
        {
            Database.NewVariable();
            UpdateDatabase();
        }

        private void button_Remove_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                ObjectHolder var = listBox1.SelectedItem as ObjectHolder;
                Database.RemoveVariable(var.Object as IGameVariable);
                UpdateDatabase();

                this.flowLayoutPanel1.Controls.Clear();
            }
        }


    }
}
